// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using ValidationAspects.Exceptions;
using ValidationAspects.Sdk;

namespace ValidationAspects.Factories
{
	public class NotEqual : NumericValidatorFactoryBase
	{
		public double Number { get; set; }
		public string Text { get; set; }
		public bool IgnoreCase { get; set; }

		#region Overrides of NumericValidatorFactoryBase
		protected override Action<T, IValidationContext> GetValidateAction<T>(Func<T, double, int> compareFunc)
		{
			return (v, c) => { if (compareFunc(v, Number) == 0) throw new ValidateNotEqualException(Number); };
		}

		public override IValidator<T> Create<T>()
		{
			if (Text == null)
				return base.Create<T>();

#if SILVERLIGHT
			return Adapt<T, string>.From(new Validator<string>((v, c) => { if (v != null && string.Compare(v, Text, IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) throw new ValidateNotEqualException(Text, IgnoreCase); }));
#else
			return Adapt<T, string>.From(new Validator<string>((v, c) => { if (v != null && string.Compare(v, Text, IgnoreCase) == 0) throw new ValidateNotEqualException(Text, IgnoreCase); }));
#endif
		}
		#endregion
	}
}